'''
Created on Feb 1, 2010

@author: madsk
'''

from PyQt4 import QtGui, QtCore
import scavenger

class MainWidget(QtGui.QWidget):
    modeChange = QtCore.pyqtSignal(str)
    
    def __init__(self, model, parent = None):
        super(MainWidget, self).__init__(parent)
        
        self._vbox = QtGui.QVBoxLayout()
        self.setLayout(self._vbox)

        # Build the UI for the monitor.
        self._table = QtGui.QTableView()
        self._table.setModel(model)
        
        # Fix the size of the columns.
        header = self._table.horizontalHeader()
        header.setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
        header.setResizeMode(1, QtGui.QHeaderView.Stretch)
        header.setResizeMode(2, QtGui.QHeaderView.ResizeToContents)
        
        # Set the selection mode.
        self._table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self._table.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        
        self._vbox.addWidget(self._table, 1)
        
        hbox = QtGui.QHBoxLayout()
        self._vbox.addLayout(hbox)
        
        self._peersButton = QtGui.QPushButton('Peers')
        self._peersButton.clicked.connect(self._handlePeersClicked)
        hbox.addWidget(self._peersButton)
        
        self._backButton = QtGui.QPushButton('Back')
        self._backButton.clicked.connect(self._handleBackClicked)
        hbox.addWidget(self._backButton)
        
    def _handleBackClicked(self):
        self.modeChange.emit('Browser')
        
    def _handlePeersClicked(self):
        msg = ''
        for peer in scavenger.Scavenger.get_peers():
            msg += str(peer)
        dialog = QtGui.QMessageBox()
        dialog.setText('Peers: %s'%msg)
        dialog.exec_()